<template>
  <div
    id="app"
    @keyup="keyup"
    @contextmenu="handleRightClick"
    tabindex="-1"
    :class="{
      night: grimoire.isNight,
      static: grimoire.isStatic,
    }"
    :style="{
      backgroundImage: grimoire.background
        ? `url('${grimoire.background}')`
        : '',
    }"
  >
    <video
      id="background"
      v-if="grimoire.background && grimoire.background.match(/\.(mp4|webm)$/i)"
      :src="grimoire.background"
      autoplay
      loop
    ></video>
    <div class="backdrop"></div>
    <transition name="blur">
      <Intro v-if="!players.length"></Intro>
      <TownInfo v-if="players.length && !session.nomination"></TownInfo>
      <Vote v-if="session.nomination"></Vote>
    </transition>
    <TownSquare @open-action-panel="openActionPanel"></TownSquare>
    <Menu ref="menu"></Menu>
    <HistoryPanel
      :isVisible="showHistoryPanel"
      @close="showHistoryPanel = false"
    />
    <PlayerActionPanel
      :isVisible="showActionPanel"
      :player="selectedPlayer"
      :playerIndex="selectedPlayerIndex"
      @close="showActionPanel = false"
      @open-role-modal="openRoleModal"
    />
    <PlayerDrawer
      :isVisible="showDrawer"
      :player="selectedPlayer"
      :playerIndex="selectedPlayerIndex"
      @close="showDrawer = false"
      @open-role-modal="openRoleModal"
    />
    
    <FabledModal />
    <RoleAbilityModal />
    <RoleModal />
    <RolesModal />
    <ReferenceModal />
    <NightOrderModal />
    <VoteHistoryModal />
    <GameStateModal />
    <ScriptBrowser
      v-if="showScriptBrowser"
      @close="showScriptBrowser = false"
    />

    <!-- 聊天面板 -->
    <ChatPanel />
    
    <!-- 语音聊天面板 -->
    <VoiceChatPanel />

    <Gradients />
    <span id="version">v{{ version }}</span>
  </div>
</template>

<script>
import { mapState } from "vuex";
import { version } from "../package.json";
import TownSquare from "./components/TownSquare";
import TownInfo from "./components/TownInfo";
import Menu from "./components/Menu";
import RolesModal from "./components/modals/RolesModal";

import Intro from "./components/Intro";
import ReferenceModal from "./components/modals/ReferenceModal";
import Vote from "./components/Vote";
import Gradients from "./components/Gradients";
import NightOrderModal from "./components/modals/NightOrderModal";
import FabledModal from "@/components/modals/FabledModal";
import VoteHistoryModal from "@/components/modals/VoteHistoryModal";
import GameStateModal from "@/components/modals/GameStateModal";
import HistoryPanel from "@/components/HistoryPanel";
import RoleAbilityModal from "@/components/modals/RoleAbilityModal";
import PlayerActionPanel from "@/components/PlayerActionPanel";
import PlayerDrawer from "@/components/PlayerDrawer";
import RoleModal from "@/components/modals/RoleModal";
import ScriptBrowser from "@/components/scripts/ScriptBrowser";
import ChatPanel from "@/components/ChatPanel";
import VoiceChatPanel from "@/components/VoiceChatPanel";

export default {
  components: {
    GameStateModal,
    VoteHistoryModal,
    FabledModal,
    NightOrderModal,
    Vote,
    ReferenceModal,
    Intro,
    TownInfo,
    TownSquare,
    Menu,

    RolesModal,
    Gradients,
    HistoryPanel,
    RoleAbilityModal,
    PlayerActionPanel,
    PlayerDrawer,
    RoleModal,
    ScriptBrowser,
    ChatPanel,
    VoiceChatPanel,
  },
  computed: {
    ...mapState(["grimoire", "session"]),
    ...mapState("players", ["players"]),
    ...mapState("voiceChat", ["users"]),
    // 获取当前用户角色（服务器数据优先）
    userRole() {
      const serverRole = this.users?.get(this.session.playerId)?.role;
      return serverRole || null;
    },
  },
  data() {
    return {
      version,
      showHistoryPanel: false,
      showActionPanel: false,
      showDrawer: false,
      selectedPlayer: null,
      selectedPlayerIndex: -1,
      showScriptBrowser: false,
    };
  },
  methods: {
    keyup({ key, altKey }) {
      // 所有快捷键都需要Alt键
      if (!altKey) return;

      switch (key.toLocaleLowerCase()) {
        case "g":
          this.$store.commit("toggleGrimoire");
          break;
        case "a":
          this.$refs.menu.addPlayer();
          break;
        case "h":
          this.$refs.menu.hostSession();
          break;
        case "j":
          this.$refs.menu.joinSession();
          break;
        case "r":
          this.$store.commit("toggleModal", "reference");
          break;
        case "n":
          this.$store.commit("toggleModal", "nightOrder");
          break;
        case "e":
          if (this.userRole !== 'storyteller') return;
          this.showScriptBrowser = true;
          break;
        case "c":
          if (this.userRole !== 'storyteller') return;
          this.$store.commit("toggleModal", "roles");
          break;
        case "v":
          if (this.session.voteHistory.length || this.userRole === 'storyteller') {
            this.$store.commit("toggleModal", "voteHistory");
          }
          break;
        case "s":
          if (this.userRole !== 'storyteller') return;
          this.$refs.menu.toggleNight();
          break;
        case "escape":
          this.$store.commit("toggleModal");
      }
    },
    openRoleModal(playerIndex) {
      this.$store.commit("toggleModal", "role");
      this.$store.commit("setSelectedPlayer", playerIndex);
    },
    openActionPanel(playerIndex) {
      this.selectedPlayer = this.players[playerIndex];
      this.selectedPlayerIndex = playerIndex;
      this.showDrawer = true;
    },

    handleRightClick(event) {
      // 隐藏的右键菜单：在空白区域右键点击
      const target = event.target;
      if (target.id === "app" || target.classList.contains("backdrop")) {
        event.preventDefault();
        // 移除右键菜单触发，改为更隐秘的方式
      }
    },
  },
  mounted() {
    // 初始化聊天模块
    if (this.$store.state.chat) {
      this.$store.dispatch('chat/initializeChat');
    }
  },
};
</script>

<style lang="scss">
@import "vars";

@font-face {
  font-family: "Papyrus";
  src: url("assets/fonts/papyrus.eot"); /* IE9*/
  src:
    url("assets/fonts/papyrus.eot?#iefix") format("embedded-opentype"),
    /* IE6-IE8 */ url("assets/fonts/papyrus.woff2") format("woff2"),
    /* chrome firefox */ url("assets/fonts/papyrus.woff") format("woff"),
    /* chrome firefox */ url("assets/fonts/papyrus.ttf") format("truetype"),
    /* chrome firefox opera Safari, Android, iOS 4.2+*/
      url("assets/fonts/papyrus.svg#PapyrusW01") format("svg"); /* iOS 4.1- */
}

@font-face {
  font-family: PiratesBay;
  src: url("assets/fonts/piratesbay.ttf");
  font-display: swap;
}

html,
body {
  font-size: 1.2em;
  line-height: 1.4;
  background: url("assets/background.jpg") center center;
  background-size: cover;
  color: white;
  height: 100%;
  font-family: "Roboto Condensed", sans-serif;
  -webkit-font-smoothing: antialiased;
  -moz-osx-font-smoothing: grayscale;
  padding: 0;
  margin: 0;
  overflow: hidden;
}

@import "media";

* {
  box-sizing: border-box;
  position: relative;
}

a {
  color: $townsfolk;
  &:hover {
    color: $demon;
  }
}

h1,
h2,
h3,
h4,
h5 {
  margin: 0;
  text-align: center;
  font-family: PiratesBay, sans-serif;
  letter-spacing: 1px;
  font-weight: normal;
}

ul {
  list-style-type: none;
  margin: 0;
  padding: 0;
}

#app {
  height: 100%;
  background-position: center center;
  background-size: cover;
  display: flex;
  align-items: center;
  align-content: center;
  justify-content: center;

  // disable all animations
  &.static *,
  &.static *:after,
  &.static *:before {
    transition: none !important;
    animation: none !important;
  }
}

#version {
  position: absolute;
  text-align: right;
  right: 10px;
  bottom: 10px;
  font-size: 60%;
  opacity: 0.5;
}

.blur-enter-active,
.blur-leave-active {
  transition: all 400ms cubic-bezier(0.4, 0, 0.2, 1);
  filter: blur(0);
  transform: scale(1);
}
.blur-enter,
.blur-leave-to {
  opacity: 0;
  filter: blur(20px);
  transform: scale(0.95);
}

// Buttons
.button-group {
  display: flex;
  align-items: center;
  justify-content: center;
  align-content: center;
  .button {
    margin: 5px 0;
    border-radius: 0;
    &:first-child {
      border-top-left-radius: 15px;
      border-bottom-left-radius: 15px;
    }
    &:last-child {
      border-top-right-radius: 15px;
      border-bottom-right-radius: 15px;
    }
  }
}
.button {
  padding: 0;
  border: solid 0.125em transparent;
  border-radius: 15px;
  box-shadow:
    inset 0 1px 1px #9c9c9c,
    0 0 10px #000;
  background:
    radial-gradient(at 0 -15%, rgba(#fff, 0.07) 70%, rgba(#fff, 0) 71%) 0 0/ 80%
      90% no-repeat content-box,
    linear-gradient(#4e4e4e, #040404) content-box,
    linear-gradient(#292929, #010101) border-box;
  color: white;
  font-weight: bold;
  text-shadow: 1px 1px rgba(0, 0, 0, 0.5);
  line-height: 170%;
  margin: 5px auto;
  cursor: pointer;
  transition: all 300ms cubic-bezier(0.4, 0, 0.2, 1);
  white-space: nowrap;
  transform: translateY(0);
  &:hover {
    color: red;
    transform: translateY(-2px);
    box-shadow:
      inset 0 1px 1px #9c9c9c,
      0 4px 15px rgba(0, 0, 0, 0.3);
  }
  &:active {
    transform: translateY(0) scale(0.98);
    transition: all 100ms;
  }
  &:focus {
    outline: none;
    box-shadow: 0 0 0 2px rgba(255, 255, 255, 0.3);
  }
  &.disabled {
    color: gray;
    cursor: default;
    opacity: 0.75;
  }
  &:before,
  &:after {
    content: " ";
    display: inline-block;
    width: 10px;
    height: 10px;
  }
  &.townsfolk {
    background:
      radial-gradient(
          at 0 -15%,
          rgba(255, 255, 255, 0.07) 70%,
          rgba(255, 255, 255, 0) 71%
        )
        0 0/80% 90% no-repeat content-box,
      linear-gradient(#0031ad, rgba(5, 0, 0, 0.22)) content-box,
      linear-gradient(#292929, #001142) border-box;
    box-shadow:
      inset 0 1px 1px #002c9c,
      0 0 10px #000;
    &:hover:not(.disabled) {
      color: #008cf7;
    }
  }
  &.demon {
    background:
      radial-gradient(
          at 0 -15%,
          rgba(255, 255, 255, 0.07) 70%,
          rgba(255, 255, 255, 0) 71%
        )
        0 0/80% 90% no-repeat content-box,
      linear-gradient(#ad0000, rgba(5, 0, 0, 0.22)) content-box,
      linear-gradient(#292929, #420000) border-box;
    box-shadow:
      inset 0 1px 1px #9c0000,
      0 0 10px #000;
  }
}

/* video background */
video#background {
  position: absolute;
  width: 100%;
  height: 100%;
  object-fit: cover;
}

/* Night phase backdrop */
#app > .backdrop {
  position: absolute;
  left: 0;
  right: 0;
  bottom: 0;
  top: 0;
  pointer-events: none;
  background: black;
  background: linear-gradient(
    180deg,
    rgba(0, 0, 0, 1) 0%,
    rgba(1, 22, 46, 1) 50%,
    rgba(0, 39, 70, 1) 100%
  );
  opacity: 0;
  transition: opacity 1s ease-in-out;
  &:after {
    content: " ";
    display: block;
    width: 100%;
    padding-right: 2000px;
    height: 100%;
    background: url("assets/clouds.png") repeat;
    background-size: 2000px auto;
    animation: move-background 120s linear infinite;
    opacity: 0.3;
    filter: hue-rotate(0deg);
    animation:
      move-background 120s linear infinite,
      hue-shift 30s ease-in-out infinite;
  }
}

@keyframes move-background {
  from {
    transform: translate3d(-2000px, 0px, 0px);
  }
  to {
    transform: translate3d(0px, 0px, 0px);
  }
}

@keyframes hue-shift {
  0%,
  100% {
    filter: hue-rotate(0deg);
  }
  50% {
    filter: hue-rotate(10deg);
  }
}

#app.night > .backdrop {
  opacity: 0.5;
}
</style>
